package br.com.nooclix.ptt.ws.application;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import br.com.nooclix.ptt.core.application.AffiliateApplication;
import br.com.nooclix.ptt.core.domain.afiliate.Affiliate;
import br.com.nooclix.ptt.core.infrastructure.util.ApplicationContextRegisterCore;
import br.com.nooclix.ptt.core.infrastructure.util.ResponseData;
import br.com.nooclix.ptt.ws.adapter.Adapter;
import br.com.nooclix.ptt.ws.adapter.impl.AffiliateAdapter;

@Path("/affiliate")
public class AffiliateRestApplication {

	private AffiliateApplication affiliateApplication;
	private Adapter<Affiliate> adapter;

	public AffiliateRestApplication() {

		affiliateApplication = ApplicationContextRegisterCore.getBean(
				"affiliateApplication", AffiliateApplication.class);
		adapter = new AffiliateAdapter();
	}

	@GET
	@Path("find/name/{code}/{name}/{offset}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response findByName(@PathParam("code") Integer code,
			@PathParam("name") String name, @PathParam("offset") int offset) {

		Affiliate affiliate = affiliateApplication.findById(code);
		ResponseData<Affiliate> responseData = affiliateApplication.findByName(
				affiliate, name, offset);
		String json = adapter.convert(responseData);
		return Response.ok(json).build();
	}

	@GET
	@Path("find/name/{name}/{offset}")
	public Response findByName(@PathParam("name") String name,
			@PathParam("offset") int offset) {
		Response response = null;
		ResponseData<Affiliate> affiliateList = affiliateApplication
				.findByName(name, offset);
		String json = adapter.convert(affiliateList);
		response = Response.ok(json).build();
		return response;

	}

	@GET
	@Path("find/criterion/{code}/{criterion}/{offset}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response findByCriterion(@PathParam("code") Integer code,
			@PathParam("criterion") String criterion,
			@PathParam("offset") int offset) {

		Affiliate affiliate = affiliateApplication.findById(code);
		ResponseData<Affiliate> responseData = affiliateApplication
				.findByCriterion(affiliate, criterion, offset);
		String json = adapter.convert(responseData);
		return Response.ok(json).build();
	}

	public static void main(String[] args) {

		System.out.println(new AffiliateRestApplication()
				.findByName("Iury", 0).getEntity().toString());

	}
}
